package com.swipemaster.elements;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import com.swipemaster.common.Bitmaps;
import com.swipemaster.common.Const;
import com.swipemaster.common.Coords;
import com.swipemaster.common.Global;
import com.swipemaster.moves.Move;

public class Rectangle extends Element
{
	public final int width, height;
	/** Scaled bitmap used to draw this element. */
	protected final Bitmap bitmap;

	/** @param x1 upper left x of rectangle. Universal units.
	 * @param y upper left y of rectangle. Universal units.
	 * @param x2 down right x of rectangle. Universal units.
	 * @param y2 down right y of rectangle. Universal units.
	 * @param move Type of move. */
	public Rectangle(double x1, double y1, double x2, double y2, Move move, Bitmap tile)
	{
		super(Coords.x(x1 + x2) / 2, Coords.y(y1 + y2) / 2, move, getMass(x1, y1, x2, y2));
		width = getWidth(x1, x2);
		height = getHeight(y1, y2);
		bitmap = Bitmaps.getTiledBitmap(width, height, tile);
	}

	/** @param x X of center. Universal units.
	 * @param y Y of center. Universal units.
	 * @param move Type of move.
	 * @param width Rectangle's width. Universal units.
	 * @param height Rectangle's height. Universal units.
	 * @param isTexture Whether we should set a texture or not. */
	public Rectangle(double x, double y, Move move, double width, double height, Bitmap tile)
	{
		this(x - (width / 2), y - (height / 2), x + (width / 2), y + (height / 2), move, tile);
	}

	protected static int getHeight(double y1, double y2)
	{
		return Coords.y(y2 - y1);
	}

	protected static double getMass(double x1, double y1, double x2, double y2)
	{
		return getWidth(x1, x2) * getHeight(y1, y2);
	}

	protected static int getWidth(double x1, double x2)
	{
		return Coords.x(x2 - x1);
	}

	@Override
	public void draw(Canvas canvas)
	{
		canvas.drawBitmap(bitmap, x - (width / 2), y - (height / 2), null);
	}

	@Override
	public boolean isCollisionWith(Element element)
	{
		if (element instanceof Rectangle)
		{
			final Rectangle rect = (Rectangle) element;
			return Collision.isRectRect(x, y, width, height, rect.x, rect.y, rect.width, rect.height);
		}
		if (element instanceof Circle)
		{
			final Circle circle = (Circle) element;
			return Collision.isRectCircle(x, y, width, height, circle.x, circle.y, circle.radius);
		}
		return false;
	}

	@Override
	public boolean isWallCollision(int direction, boolean wallCollision)
	{
		int tmpWidth, tmpHeight;
		if (!wallCollision)
		{
			tmpWidth = width;
			tmpHeight = height;
		}
		else
		{
			tmpWidth = -width;
			tmpHeight = -height;
		}

		if (direction == Const.LEFT)
		{
			if ((x - (tmpWidth / 2)) <= 1)
			{
				return true;
			}
		}
		else if (direction == Const.RIGHT)
		{
			if ((x + (tmpWidth / 2)) >= (Global.app.consts.width - 1))
			{
				return true;
			}
		}
		else if (direction == Const.UP)
		{
			if ((y - (tmpHeight / 2)) <= 1)
			{
				return true;
			}
		}
		else if (direction == Const.DOWN)
		{
			if ((y + (tmpHeight / 2)) >= (Global.app.consts.height - 1))
			{
				return true;
			}
		}
		return false;
	}

}
